/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

/**
 *
 * @author mike.boddin
 */
public class Timer {
    private static long nanotime=-1;
    private static boolean isRunning=false;

    /**
     * starts timer, reset and start, if it is already running
     */
    public static void start(){
        nanotime=System.nanoTime();
        isRunning=true;
    }

    /**
     * stops timer and save the runningtime in nanoseconds
     * @throws Exception if Timer was not running
     */
    public static void stop(){
        if(isRunning){
            nanotime=System.nanoTime()-nanotime;
            isRunning=false;
        }else
            throw new RuntimeException("Timer was not running");
    }

    public static boolean isRunning(){
        return isRunning;
    }

    /**
     * @return last measured time or the actual interim (Zwischenzeit)
     * if timer is running
     */
    public static long getTimeInSeconds(){
        if(isRunning)
            return (System.nanoTime()-nanotime)/1000000000;
        else
            return nanotime/1000000000;
    }
}
